package cn.com.yint.booklist.web;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
//import java.net.URLDecoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.yint.booklist.model.WishList;
import cn.com.yint.booklist.dao.PageResult;
import cn.com.yint.booklist.dao.WishListDb;;

/**
 * Servlet implementation class WishListController
 */
@WebServlet("/mana")
public class WishListController extends HttpServlet {
	private static final long serialVersionUID = 1L;
     private static int signal=2;  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WishListController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String action=request.getParameter("action");
		switch(action){
		    case "add":addWishList(request);break;
		    case "query":queryWishList(request,response);break;
		    case "delete":deleteWishList(request);break;
		    case "update":updateWishList(request);break;
		    case "queryid":queryidWishList(request,response);break;
		    case "pager":pageR(request,response);break;
		    default:;
		
		}
		
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		//response.getWriter().append("success!");
		if(signal!=2)
		{List<WishList> list = WishListDb.getTen(1);
		PageResult pageresult=WishListDb.getPage();
		int totalpage=pageresult.getTotalPage();
		int currentpage=1;
		
		request.setAttribute("list", list);
		request.setAttribute("totalpage", totalpage);
		request.setAttribute("currentpage", currentpage);
		request.setAttribute("signal", signal);
		request.getRequestDispatcher("/getall.jsp").forward(request, response);
		}
	}

	
	private void addWishList(HttpServletRequest request) {
		
		WishList wish = new WishList();
		String bookname=request.getParameter("bookname");
		String author=request.getParameter("author");
		String publisher=request.getParameter("publisher");
		//int amount=Integer.parseInt(request.getParameter("amount"));
		String amount1=request.getParameter("amount");
		int amount=Integer.parseInt(amount1);
		String comment=request.getParameter("comment");
		String date=request.getParameter("date");
		try {
			 bookname = new String(bookname.getBytes("iso-8859-1"), "UTF-8");
			 author = new String(author.getBytes("iso-8859-1"), "UTF-8");
			 publisher = new String(publisher.getBytes("iso-8859-1"), "UTF-8");
			 comment = new String(comment.getBytes("iso-8859-1"), "UTF-8");
			 date = new String(date.getBytes("iso-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		wish.setBookname(bookname);
		wish.setAuthor(author);
		wish.setPublisher(publisher);
		wish.setAmount(amount);
		wish.setComment(comment);
		wish.setDate(date);


		if(WishListDb.insert(wish))
			signal=1;
		else
			signal=0;
		
	}
	private void queryWishList(HttpServletRequest request,HttpServletResponse response){
		signal=2;
		String bookname=request.getParameter("key");

		try {
			bookname = new String(bookname.getBytes("iso-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		List<WishList> list=WishListDb.query(bookname);
		int currentpage=1;
		request.setAttribute("list", list);
		request.setAttribute("currentpage", currentpage);
		try {
			request.getRequestDispatcher("/getall.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void deleteWishList(HttpServletRequest request){
		
		String id1=request.getParameter("id");
		int id=Integer.parseInt(id1);
		//int id=Integer.parseInt(request.getParameter("id"));
		//System.out.println(id);
		if(WishListDb.delete(id))
			signal=1;
		else
			signal=0;
		
	}
	private void updateWishList(HttpServletRequest request){
		
		WishList wish = new WishList();
		int id=Integer.parseInt(request.getParameter("id"));
		String bookname=request.getParameter("bookname");
		String author=request.getParameter("author");
		String publisher=request.getParameter("publisher");
		int amount=Integer.parseInt(request.getParameter("amount"));
		//String amount1=request.getParameter("amount");
		//int amount=Integer.parseInt(amount1);
		String comment=request.getParameter("comment");
		String date=request.getParameter("date");
		try {
			 bookname = new String(bookname.getBytes("iso-8859-1"), "UTF-8");
			 author = new String(author.getBytes("iso-8859-1"), "UTF-8");
			 publisher = new String(publisher.getBytes("iso-8859-1"), "UTF-8");
			 comment = new String(comment.getBytes("iso-8859-1"), "UTF-8");
			 date = new String(date.getBytes("iso-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		wish.setId(id);
		wish.setBookname(bookname);
		wish.setAuthor(author);
		wish.setPublisher(publisher);
		wish.setAmount(amount);
		wish.setComment(comment);
		wish.setDate(date);
		//DbUtil.update(wish);
		if(WishListDb.update(wish))
			signal=1;
		else
			signal=0;
	}
	private void queryidWishList(HttpServletRequest request,HttpServletResponse response){
		signal=2;
		int id=Integer.parseInt(request.getParameter("id"));
		WishList wish=WishListDb.queryID(id);
		request.setAttribute("wish", wish);
		
		try {
			request.getRequestDispatcher("/updatemsg.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void pageR(HttpServletRequest request,HttpServletResponse response){
		signal=2;
		int pagenumber=Integer.parseInt(request.getParameter("pagenumber"));
		int totalpage=WishListDb.getPage().getTotalPage();
		System.out.println(pagenumber);
		if(pagenumber<1)
			pagenumber=1;
		else if(pagenumber>totalpage)
			pagenumber=totalpage;
		List<WishList> list=WishListDb.getTen(pagenumber);
		
		request.setAttribute("totalpage", totalpage);
		request.setAttribute("list", list);
		request.setAttribute("currentpage", pagenumber);
		
		try {
			request.getRequestDispatcher("/getall.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}


}

